<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Input;
use App\Models\Recommend;
use App\Models\RecommendPosition;
use App\Http\Requests\Admin\Recommend\UpdateRequest;
use App\Http\Requests\Admin\Recommend\StoreRequest;
use Gate;
use App\Exceptions\Admin\PermissionDeniedException;
use App\Commands\OperationLogCommand;


class RecommendController extends Controller
{
    public function recommend_gate()
    {
        if (!Gate::allows('goods','')) {
            throw new PermissionDeniedException();
        }//end if()

    }//end func __construce()

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $this->recommend_gate();

        $recommend_position_id = $request->get("id");

        //$recommendPosition = RecommendPosition::paginate(10);

        $recommend = Recommend::orderBy('sort_order','desc')->where('recommend_position_id',$recommend_position_id)->paginate(10);

        return view('admin.recommend.index',compact('recommend','recommend_position_id'));
    }


    public function create(Request $request,Recommend $recommend)
    {

        $this->recommend_gate();

        //echo '<BR>id:'.$_REQUEST['id'];
        $recommend_position_id = $request->get('id');
        return view('admin.recommend.create',compact('recommend','recommend_position_id'));
    }


    public function store(Recommend $recommend,Request $request)
    {
        $this->recommend_gate();

        //$param = $storeRequest->all();

        //dd($request->all());
        $recommend = $recommend->create($request->all());
        $this->dispatch(
            new OperationLogCommand('添加推荐商品','Recommend','id:'.$recommend->id)
        );
        return Redirect($request->get('back_url'));
    }


    public function show($id)
    {
        //
    }


    public function edit($id)
    {
        $this->recommend_gate();

        $recommend = Recommend::find($id);
        return view('admin.recommend.edit',compact('recommend'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update($id, UpdateRequest $updateRequest)
    {
        $this->recommend_gate();

        $recommend = Recommend::find($id);

        $recommend->update($updateRequest->all());

        return Redirect($updateRequest->get("back_url"));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request , $id)
    {
        $this->recommend_gate();

        //dd($request->all());
        $return = ['status'=>0, 'message'=>'', 'data'=>''];
        $recommend = Recommend::find($id);

        $recommend->delete();
        return $return;
  
        //return Redirect($request->get("url"));
    }

    /**
     * 排序
     * @route admin.goods_attribute.sort
     * @return Response
     */
    public function sort(Request $request)
    {

        $this->recommend_gate();

        $url = $request->get('url');

        $recommend = $request->get('sort_order');

        if (is_array($recommend)&&count($recommend)>0)
        {
            foreach($recommend as $k=>$v)
            {
 
                $sort_order = Recommend::where('sort_order',$v)->get();

                Recommend::find($k)->update(['sort_order' => $v]);

            }
        }

        return redirect($url);

    }
}
